package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    @SuppressWarnings("all")
    private AttrDao attrDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params,Long categoryId) {

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!categoryId.equals(0L)){
            wrapper.eq("catelog_id",categoryId);
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
              wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> withattr(Long categoryId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id",categoryId);
        List<AttrGroupEntity> grouList = baseMapper.selectList(wrapper);
        List<AttrGroupVo> collect = grouList.stream().map(g -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(g, attrGroupVo);

            QueryWrapper<AttrEntity> w = new QueryWrapper<>();
            w.eq("attr_type", 1);
            w.eq("attr_group_id", g.getAttrGroupId());
            List<AttrEntity> attrs = attrDao.selectList(w);
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;

        }).collect(Collectors.toList());


        return collect;
    }

}